<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from '@/utils/validate'
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        mobile: '',
        factoryId: ''
      },
      optionProps:{
        value:'id',
        label:'name',
        children:'secondRegions'
      },
      dataRule: {
        name: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        roleIdList:[
          { required: true, message: '角色不能为空', trigger: 'blur' }
        ],
        regionId: [
          { required: true, message: '所属城市不能为空', trigger: 'blur' }
        ],
        projectIdList:[
          { required: true, message: '所属项目不能为空', trigger: 'blur' }
        ],
        departmentId:[
          { required: true, message: '所属部门不能为空', trigger: 'blur' }
        ],
        corporateName:[
          { required: true, message: '公司不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (factoryId,row) {
      console.log(row)
      this.dataForm.factoryId=factoryId
      if(row){
        this.dataForm = row
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    // 表单提交
    dataFormSubmit () {
      console.log(this.dataForm.regionId)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(this.dataForm.id?`/v1/factory/user/update`:'/v1/factory/user/add'),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'factoryId':this.dataForm.factoryId,
              'name': this.dataForm.name,
              'mobile': this.dataForm.mobile
            })
          }).then(({data}) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
